<?php

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------
// Initialize Installer
// ------------------------------------------------------------------------

// Show all errors
error_reporting(E_ALL);
ini_set('display_errors','On');

// Install starts here and nowhere else
define('INSTALL', TRUE);

// PHP Only
define('EXT', '.php');

// Start Session
session_start();

// Error log
$error_log = array();

// ------------------------------------------------------------------------
// Pre-Install Checks
// ------------------------------------------------------------------------

/* PHP Version check */
if (version_compare(PHP_VERSION, '5.0.0') < 0)
{
	$error_log[] = "Incompatible PHP Version.<br/><table cellspacing='0' cellpadding='0' style='padding-left: 1em;'>
					<tr> <td><strong>Required:</strong></td>  <td>5.0.0 or greater</td> </tr>
					<tr> <td><strong>Installed:</strong></td> <td>".PHP_VERSION.'</td> </tr></table>';
}


/* No Schema, no go */
if ( ! file_exists('simply_post.sql') )
{
	$error_log[] = 'Missing Database Schema';
}

/* Check for installer files */
$files = array('header', 'footer', 'functions');

foreach($files as $file)
{
	if( ! file_exists('includes/'.$file.EXT) )
	{
		$error_log[] = 'Missing Installer File: '.$file.EXT;
	}
}

/* Very basic write permissions check, useless on windows */
if ( !is_writable('../application/config/') )
{
	$error_log[] = 'No write permissions for /application/config/';
}

// ------------------------------------------------------------------------
// Show Errors
// ------------------------------------------------------------------------

if ( count($error_log) > 0)
{
	echo '<h3>=== Error Log ===</h3>';
	echo '<ul>';
	
	foreach($error_log as $error)
	{
		echo '<li><p>'.$error.'</p></li>';
	}
	
	echo '</ul>';
	exit;
}

// ------------------------------------------------------------------------
// No Errors, include functions
// ------------------------------------------------------------------------


include('includes/functions'.EXT);


// ------------------------------------------------------------------------
// Figure out step and set correct file
// ------------------------------------------------------------------------

// Safe progress tracking
$sess_step = ( isset($_SESSION) AND isset($_SESSION['step']) ) ? $_SESSION['step'] : 'welcome';
$uri_step = ( is_array($_GET) AND count($_GET) == 1 ) ? key($_GET) : 'welcome';

// If they don't match it may be a hacking attempt
if($sess_step != $uri_step)
{
	// This check is done in the first step - a step missmatch indicates an attempt to get around
	// this check, so we force it.
	if ( check_complete_install() === TRUE )
		die('SimplyPost is already installed.');
}

// URI segment dedicates the current step in case they hit the back button

switch($uri_step)
{
	case 'license':
		$file = 'license';
		break;
	case 'server';
		$file = 'server_info';
		break;
	case 'database':
		$file = 'db_info';
		break;
	case 'user':
		$file = 'user_info';
		break;
	case 'complete':
		$file = 'success';
		break;
	case 'welcome':
	default:
		$uri_step = 'welcome';
		$file = 'welcome';
		break;
}

// ------------------------------------------------------------------------
// Include Files
// ------------------------------------------------------------------------

$title = ucfirst($uri_step);
include('includes/header'.EXT);
include('includes/'.$file.EXT);
include('includes/footer'.EXT);

?>